<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<!--引入JSTL核心标签库  core-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
.fa-refresh:before {
	content: "\f021";

}
.pageTitle{

width: 80px;
height: 40px;
font-family: "华文行楷";
font-size: 40px;

}
</style>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- Font Awesome -->
<link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">

<!-- Ionicons -->
<link rel="stylesheet"
	href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<!-- Tempusdominus Bbootstrap 4 -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
<!-- iCheck -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
<!-- JQVMap -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/jqvmap/jqvmap.min.css">
<!-- Theme style -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/css/adminlte.min.css">
<!-- overlayScrollbars -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
<!-- Daterange picker -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/daterangepicker/daterangepicker.css">
<!-- summernote -->
<link rel="stylesheet"
	href="${pageContext.servletContext.contextPath}/plugins/summernote/summernote-bs4.css">
<!-- Google Font: Source Sans Pro -->
<!--   <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
 -->
</head>
<body>

	<div class="container-fluid">
		<div class="row">
			<div class="col-12">
				<div class="card">
					<div class="card-header">
						<span class="pageTitle">学生信息管理</span>
						<span ><i class="fa fa-refresh fa-fw" onclick="refresh()"></i> </span>
						<div class="card-tools">
							<div class="input-group input-group-sm" style="width: 150px;">
								<button type="button"
									class="btn btn-block btn-outline-secondary"
									style="margin-bottom: 2%; height: 35px;"
									onclick="openAddStudent()">新增学生</button>

								<input id="selectUser" type="text" name="table_search"
									class="form-control float-right" placeholder="姓名或学号">
								<div class="input-group-append">
									<button type="submit" class="btn btn-default"
										onclick="selectUserByName()">
										<i class="fas fa-search"></i>
									</button>
								</div>

							</div>
						</div>

					</div>
					<!-- /.card-header -->
					<div class="card-body table-responsive p-0" style="height: 80%;">
						<table class="table table-head-fixed text-nowrap">
							<thead>
								<tr>
									<th>编号</th>
									<th>学号</th>
									<th>姓名</th>
									<th>密码</th>
									<th>性别</th>
									<th>年龄</th>
									<th>班级</th>
									<th>邮箱</th>
									<th>状态</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody>
								<c:forEach items="${list }" var="s">
									<tr>
										<td>${s.s_id }</td>
										<td>${s.s_no }</td>
										<td>${s.s_name }</td>
										<td>*****</td>
										<td>${s.gender }</td>
										<td>${s.age }</td>
										<td>${s.c_name }</td>
										<td>${s.email }</td>
										<td>
											<c:if test="${s.isenabled eq 1 }">
												<span class="badge bg-success">在读</span>
											</c:if>
											<c:if test="${s.isenabled eq 0 }">
												<span class="badge bg-danger">毕业</span>
											</c:if>
											<c:if test="${s.isenabled eq 3 }">
												<span class="badge bg-primary">休学</span>
											</c:if>
										</td>
										<td>

											<button type="button" class="btn btn-warning btn-sm"
												onclick="openModifyStudent(${s.s_id })">修改</button>
											<button type="button" class="btn btn-danger btn-sm"
												onclick="deleteStudent(${s.s_id })">删除</button>
										</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
					<!-- /.card-body -->
				</div>
				<!-- /.card -->
			</div>
		</div>
	</div>

	<!-- 新增用户模态框 -->
	<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog"
		aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">新增学生</h5>
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body">
					<form>
						<div class="form-group">
							<label for="email" class="col-form-label" >学号:</label> <input
								type="text" class="form-control" id="s_no" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
					
						<div class="form-group">
							<label for="uname" class="col-form-label">姓名:</label> <input
								type="text" class="form-control" id="s_name" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">密码:</label> <input
								type="text" class="form-control" id="s_pass" maxlength="19" placeholder="最多允许输入19个字符" >
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">性别:</label> 
							<select class="custom-select rounded-0" id="gender">
			                    <option value="男">男</option>
			                    <option value="女">女</option>
			                 </select>
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">年龄:</label> <input
								type="text" class="form-control" id="age"  placeholder="输入年龄,最小值0，最大值999" oninput = "value=value.replace(/[^\d]/g,'')" maxlength="3">
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="email" class="col-form-label">邮箱:</label> <input
								type="text" class="form-control" id="email" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
						<div class="form-group">
							<label for="email" class="col-form-label">班级:</label> 
							
							<select class="custom-select rounded-0" id="c_name">
								<c:forEach items="${classNameList }" var="cnl">
				                    <option value="${cnl.c_id }">${cnl.c_name }</option>
			                    </c:forEach>
			                  </select>
							
						</div>
						<div >
							<label for="email" class="col-form-label">状态:</label>
							<select class="custom-select rounded-0" id="isenabled">
			                    <option value="1">在读</option>
			                    <option value="3">休学</option>
			                    <option value="0">毕业</option>
			                  </select>
						</div>
						
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary"
						data-dismiss="modal">关闭</button>
					<button type="button" onclick="addStudent()" class="btn btn-primary">保存</button>
				</div>
			</div>
		</div>
	</div>

	<!-- 修改用户模态框 -->
	<div class="modal fade" id="exampleModal02" tabindex="-1" role="dialog"
		aria-labelledby="exampleModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<h5 class="modal-title" id="exampleModalLabel">修改用户</h5>
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
				</div>
				<div class="modal-body">
					<form>
						<input type="hidden" id="r_s_id">
						<div class="form-group">
							<label for="email" class="col-form-label" >学号:</label> <input
								type="text" class="form-control" id="r_s_no" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
					
						<div class="form-group">
							<label for="uname" class="col-form-label">姓名:</label> <input
								type="text" class="form-control" id="r_s_name" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">密码:</label> <input
								type="text" class="form-control" id="r_s_pass" maxlength="19" placeholder="最多允许输入19个字符" >
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">性别:</label> 
							<select class="custom-select rounded-0" id="r_gender">
			                    <option value="男">男</option>
			                    <option value="女">女</option>
			                 </select>
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="upass" class="col-form-label">年龄:</label> <input
								type="text" class="form-control" id="r_age"  placeholder="输入年龄,最小值0，最大值999" oninput = "value=value.replace(/[^\d]/g,'')" maxlength="3">
							<!-- <textarea class="form-control" id="message-text"></textarea> -->
						</div>
						<div class="form-group">
							<label for="email" class="col-form-label">邮箱:</label> <input
								type="text" class="form-control" id="r_email" maxlength="19" placeholder="最多允许输入19个字符" >
						</div>
						<div class="form-group">
							<label for="email" class="col-form-label">班级:</label> 
							
							<select class="custom-select rounded-0" id="r_c_name">
								<c:forEach items="${classNameList }" var="cnl">
				                    <option value="${cnl.c_id }">${cnl.c_name }</option>
			                    </c:forEach>
			                  </select>
							
						</div>
						<div >
							<label for="email" class="col-form-label">状态:</label>
							<select class="custom-select rounded-0" id="r_isenabled">
			                    <option value="1">在读</option>
			                    <option value="3">休学</option>
			                    <option value="0">毕业</option>
			                  </select>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-secondary"
						data-dismiss="modal">关闭</button>
					<button type="button" onclick="saveModifyStudent()"
						class="btn btn-primary">保存</button>
				</div>
			</div>
		</div>
	</div>

</body>
</html>
<!-- jQuery -->
<script
	src="${pageContext.servletContext.contextPath}/plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script
	src="${pageContext.servletContext.contextPath}/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script
	src="${pageContext.servletContext.contextPath}/js/adminlte.min.js"></script>


<script>
	//添加用戶弹窗
	function openAddStudent(){
		
		$('#exampleModal').modal({
			  keyboard: false
		});
	}
	
	//Ajax进行添加用户
	function addStudent(){
		//从表单获取数据
		var s_no=$("#s_no").val();
		var s_name=$("#s_name").val();
		var s_pass=$("#s_pass").val();
		var gender=$("#gender").val();
		var age=$("#age").val();
		var c_id=$("#c_name").val();
		var email=$("#email").val();
		var isenabled=$("#isenabled").val();
		
		
		if(s_no!=null&&s_no!=''&&s_name!=null&&s_name!=''&&s_pass!=null&&s_pass!=''
				&&gender!=null&&gender!=''&&age!=null&&age!=''&&c_id!=null
				&&c_id!=''&&isenabled!=null&&isenabled!=''&&email!=null&&email!=''){
			
			//异步方式调用
			$.ajax({
				url:"addStudent.do",
				data:{"s_no":s_no,"s_name":s_name,"s_pass":s_pass,"gender":gender,"age":age,"c_id":c_id,"email":email,"isenabled":isenabled},
				success:function(data){
					if(data.code=="10000"){
						alert("添加成功");
						window.location.href="studentList.do";
					}
					else{
						alert("添加失败");
					}
				}
			});
			
			
		}else{
			alert("信息未填写完善，请重新检查！");
		}
		
		
		
	}
	
	//Ajax进行删除用户
	function deleteStudent(s_id){

		var msg = "确认删除该用户信息吗？\n注意：该操作无法恢复！";
        if (confirm(msg)==true){
           
        	$.ajax({
				url:"deleteStudent.do",
				data:{"s_id":s_id},
				success:function(data){
					if(data.code=="10000"){
						alert("删除成功");
						window.location.href="studentList.do";
					}
					else{
						alert("删除失败");
					}
				}
			});
        	

        }else{
        	alert("撤销删除操作成功");
        }

		
		}
	//Ajax进行读取用户信息，显示弹窗，打印到控件
	function openModifyStudent(id){
		
		
		$.ajax({
			url:"selectStudentById.do",
			data:{"id":id},
			success:function(data){
				
				if(data.code=="10000"){
				
					$("#r_s_no").val(data.data.s_no);
					$("#r_s_name").val(data.data.s_name);
					$("#r_s_pass").val(data.data.s_pass);
					$("#r_gender").val(data.data.gender);
					$("#r_email").val(data.data.email);
					$("#r_age").val(data.data.age);
					$("#r_c_name").val(data.data.c_id);
					$("#r_isenabled").val(data.data.isenabled);
					$("#r_s_id").val(data.data.s_id);

				}
				$('#exampleModal02').modal({
					  keyboard: false
				});	
					
			}
		});
		
		
	}
	//Ajax修改用户
	function saveModifyStudent(){
		
		//从表单获取数据
		var s_id=$("#r_s_id").val();
		var s_no=$("#r_s_no").val();
		var s_name=$("#r_s_name").val();
		var s_pass=$("#r_s_pass").val();
		var gender=$("#r_gender").val();
		var age=$("#r_age").val();
		var c_id=$("#r_c_name").val();
		var email=$("#r_email").val();
		var isenabled=$("#r_isenabled").val();
		//异步方式调用
		$.ajax({
			url:"updateStudent.do",
			data:{"s_id":s_id,"s_no":s_no,"s_name":s_name,"s_pass":s_pass,"gender":gender,"age":age,"c_id":c_id,"email":email,"isenabled":isenabled},
			success:function(data){
				if(data.code=="10000"){
					alert("更新成功");
					window.location.href="studentList.do";
				}
				else{
					alert("更新失败");
				}
			}
		});
		
		
	}
	
	function selectUserByName(){
		var s_name=$(selectUser).val();
		window.location.href="studentList.do?s_name="+s_name;
	}
	
	function refresh(){
		window.location.href="studentList.do";
	}
	
	

	
</script>



